package com.training.schedule_management_system_admin.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.training.schedule_management_system_admin.constant.Msg;
import com.training.schedule_management_system_admin.entity.Scheduleframework;
import com.training.schedule_management_system_admin.entity.Scheduleterm;
import com.training.schedule_management_system_admin.entity.User;
import com.training.schedule_management_system_admin.entity.VO.ScheduleframeworkVO;
import com.training.schedule_management_system_admin.entity.VO.ScheduletermListVO;
import com.training.schedule_management_system_admin.entity.VO.ScheduletermVO;
import com.training.schedule_management_system_admin.service.ScheduleframeworkService;
import com.training.schedule_management_system_admin.service.UserService;
import com.training.schedule_management_system_admin.utils.DateListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    ScheduleframeworkService scheduleframeworkService;

    /*
    主页
    */
    @RequestMapping("/index")
    public String toIndex(){
        return "user/index";
    }

    /*
    获取所有已开放共享的日程框架
     */
    @ResponseBody
    @RequestMapping("/frameworks")
    public Msg getFrameworks(){
        List<Scheduleframework> list=scheduleframeworkService.getFrameworks();
        return Msg.success().add("frameworks",list);
    }

    /*
    当前用户下的日程框架列表
     */
    @RequestMapping("/toScheduleframework")
    public String toFramework(@RequestParam(value = "pageNum",defaultValue ="1") int pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                              @RequestParam(value = "navigatePages",defaultValue = "5") int navigatePages,
                              Model model){
        JSONObject  jsonObject=new JSONObject();
        PageHelper.startPage(pageNum, pageSize);
        List<Scheduleframework> frameworks=scheduleframeworkService.getAllWithUser();
        PageInfo page=new PageInfo(frameworks,navigatePages);
        jsonObject.put("page",page);
        model.addAttribute("msg",jsonObject);
        model.addAttribute("sfVO",new ScheduleframeworkVO());
        model.addAttribute("stVO",new ScheduletermListVO());
        return "/user/scheduleframework";
    }

    /*
    得到日程框架
     */
    @ResponseBody
    @RequestMapping("/getFramework/{frameworkId}")
    public Msg getFrameworkAndTerms(@PathVariable("frameworkId")Integer frameworkId){
        Scheduleframework framework = scheduleframeworkService.getFrameworkAndTerm(frameworkId);
        if(framework==null){
            return Msg.fail();
        }

        ScheduleframeworkVO sfVO = new ScheduleframeworkVO();
        sfVO.setId(String.valueOf(framework.getId()));
        sfVO.setName(framework.getName());
        sfVO.setOrganization(framework.getOrganization());
        sfVO.setStartDate(DateUtil.format(framework.getStartDate(),"yyyy-MM-dd"));
        sfVO.setEndDate(DateUtil.format(framework.getEndDate(),"yyyy-MM-dd"));

        List<ScheduletermVO> stVOS=new ArrayList<>();
        for(Scheduleterm term:framework.getScheduleterms()){
            ScheduletermVO stVO=new ScheduletermVO();
            stVO.setOrderNum(String.valueOf(term.getOrderNum()));
            stVO.setStartTime(DateUtil.format(term.getStartTime(),"HH:mm"));
            stVO.setEndTime(DateUtil.format(term.getEndTime(),"HH:mm"));
            stVOS.add(stVO);
        }

        return Msg.success().add("framework",framework).add("sfVO",sfVO).add("stVOS",stVOS);
    }

    /*
    查看日程框架
     */
    @RequestMapping("/view/{scheduleframeworkId}")
    public String view(@PathVariable("scheduleframeworkId")Integer scheduleframeworkId, Model model, HttpSession session){
        JSONObject  jsonObject=new JSONObject();
        Scheduleframework framework= scheduleframeworkService.getFrameworkAndTerm(scheduleframeworkId);
        if(framework==null){
            jsonObject.put("msg","该日程框架不存在!");
            model.addAttribute("result",jsonObject);
            return "/user/result";
        }
        //草稿状态的日程框架对其它用户不可见!
        if(!framework.getUserId().equals(((User) session.getAttribute("loginUser")).getId())){
            if(framework.getDraftState()==1) {
                jsonObject.put("msg", "该日程框架不存在!");
                model.addAttribute("result", jsonObject);
                return "/user/result";
            }
        }
        jsonObject.put("result",framework);
        String startDate = DateUtil.format(framework.getStartDate(),"yyyy-MM-dd");
        String endDate = DateUtil.format(framework.getEndDate(),"yyyy-MM-dd");
        List<String> days = DateListUtils.getDays(startDate, endDate);
        jsonObject.put("days",days);
        jsonObject.put("size",days.size());
        model.addAttribute("msg",jsonObject);
        return "/user/scheduleframework_view";
    }


    /*
    新增日程框架和日程框架项
     */
    @PostMapping("/createFramework")
    public String createFramework(@ModelAttribute("sfVO") ScheduleframeworkVO sfVO,
                                  @ModelAttribute("stVO") ScheduletermListVO stVO,
                                  Model model){
        JSONObject jsonObject=new JSONObject();
        scheduleframeworkService.saveFrameworkAndTerm(sfVO,stVO.getStVOS());
        jsonObject.put("msg","保存成功!");
        model.addAttribute("result",jsonObject);
        return "/user/result";
    }

    /*
    修改日程框架和日程框架项
     */
    @PostMapping("/updateFramework")
    public String updateFramework(@ModelAttribute("sfVO") ScheduleframeworkVO sfVO,
                                  @ModelAttribute("stVO") ScheduletermListVO stVO,
                                  Model model){
        JSONObject jsonObject=new JSONObject();
        scheduleframeworkService.updateFrameworkAndTerm(sfVO,stVO.getStVOS());
        jsonObject.put("msg","修改成功!");
        model.addAttribute("result",jsonObject);
        return "/user/result";
    }

    /*
    查询
     */
    @RequestMapping("/searchFramework")
    public String searchFramework(@RequestParam(value = "pageNum",defaultValue ="1") int pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "500") int pageSize,
                                  @RequestParam(value = "navigatePages",defaultValue = "5") int navigatePages,
                                  String value,Model model){
        JSONObject jsonObject=new JSONObject();
        PageHelper.startPage(pageNum, pageSize);
        List<Scheduleframework> scheduleframeworks=scheduleframeworkService.findFrameworkLike(value);
        if(scheduleframeworks.isEmpty()){
            jsonObject.put("msg","没有找到包含该名字的日程框架");
            model.addAttribute("framework_result",jsonObject);
            return "/user/search";
        }
        PageInfo page=new PageInfo(scheduleframeworks,navigatePages);
        jsonObject.put("page",page);
        model.addAttribute("msg",jsonObject);
        return "/user/scheduleframework";
    }
}
